package com.teamone.etl.util;

import java.security.Key;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

/**
 * @ClassName: DesUtil
 * @Author xqing
 * @Date 2021/1/7
 */
public class DesUtil {
    private Key key;
    private static final String DES = "DES";
    public static final String[] KEY_ARR = { "IeJSB9d1lI02fswfds2", "zafZ6khrle5W8hATjKBAqQ",
              "so7uFYz4n0yk3faYxVHhiRB" };
    /**
     * <p>Title: 密匙</p>
     * <p>Description: </p>
     * @param str
     */
    public DesUtil(String str) {
        // 生成密匙
        setKey(str);
    }
    /**
     *
     * @Title: setKey
     * @Description: 根据参数生成 KEY
     * @param strKey
     * @author chenxy
     * @date 2018-4-4 上午11:14:46
     */
    public void setKey(String strKey) {
        try {
            KeyGenerator generator = KeyGenerator.getInstance(DES);
            generator.init(new SecureRandom(strKey.getBytes()));
            this.key = generator.generateKey();
            generator = null;
        } catch (Exception e) {
            throw new RuntimeException("Error initializing SqlMap class. Cause: " + e);
        }
    }

    /**
     * @Title: encryptStr
     * @Description: 加密 String, 明文输入, String密文输出
     * @param strMing
     * @return
     * @author chenxy
     * @date 2018-4-4 上午11:16:04
     */
    public String encryptStr(String strMing) {
        byte[] byteMi = null;
        byte[] byteMing = null;
        String strMi = "";
        BASE64Encoder base64en = new BASE64Encoder();
        try {
            byteMing = strMing.getBytes("UTF8");
            byteMi = this.encryptByte(byteMing);
            strMi = base64en.encode(byteMi);
        } catch (Exception e) {
            throw new RuntimeException(
                    "Error initializing SqlMap class. Cause: " + e);
        } finally {
            base64en = null;
            byteMing = null;
            byteMi = null;
        }
        return strMi;
    }

    /**
     * @Title: decryptStr
     * @Description: 解密, 以String密文输入, String明文输出
     * @param strMi
     * @return
     * @author chenxy
     * @date 2018-4-4 上午11:16:32
     */
    public String decryptStr(String strMi) {
        BASE64Decoder base64De = new BASE64Decoder();
        byte[] byteMing = null;
        byte[] byteMi = null;
        String strMing = "";
        try {
            byteMi = base64De.decodeBuffer(strMi);
            byteMing = this.decryptByte(byteMi);
            strMing = new String(byteMing, "UTF8");
        } catch (Exception e) {
            throw new RuntimeException("Error initializing SqlMap class. Cause: " + e);
        } finally {
            base64De = null;
            byteMing = null;
            byteMi = null;
        }
        return strMing;
    }

    /**
     * @Title: encryptByte
     * @Description: 加密, 以byte[]明文输入, byte[]密文输出
     * @param byteS
     * @return
     * @author chenxy
     * @date 2018-4-4 上午11:17:08
     */
    private byte[] encryptByte(byte[] byteS) {
        byte[] byteFina = null;
        Cipher cipher;
        try {
            cipher = Cipher.getInstance(DES);
            cipher.init(Cipher.ENCRYPT_MODE, key);
            byteFina = cipher.doFinal(byteS);
        } catch (Exception e) {
            throw new RuntimeException("Error initializing SqlMap class. Cause: " + e);
        } finally {
            cipher = null;
        }
        return byteFina;
    }

    /**
     *
     * @Title: decryptByte
     * @Description: 解密, 以byte[]密文输入, 以 byte[] 明文输出
     * @param byteD
     * @return
     * @author chenxy
     * @date 2018-4-4 上午11:17:39
     */
    private byte[] decryptByte(byte[] byteD) {
        Cipher cipher;
        byte[] byteFina = null;
        try {
            cipher = Cipher.getInstance(DES);
            cipher.init(Cipher.DECRYPT_MODE, key);
            byteFina = cipher.doFinal(byteD);
        } catch (Exception e) {
            throw new RuntimeException("Error initializing SqlMap class. Cause: " + e);
        } finally {
            cipher = null;
        }
        return byteFina;
    }





}
